package main

import (
	"fmt"
	"sort"
)

func main() {
	//找出下标和为8的两个数
	var arr = [...]int{1, 3, 5, 7, 8}
	for i := 0; i < len(arr); i++ {
		for j := i + 1; j < len(arr); j++ {
			if arr[i]+arr[j] == 8 {
				fmt.Println("i=", i, "j=", j)
			}
		}
	}
	/*
		选择排序：进行从小到大排序 [9,4,6,1,8,7]
	*/
	var arr2 = []int{9, 4, 6, 1, 8, 7}
	for i := 0; i < len(arr2); i++ {
		for j := i + 1; j < len(arr2); j++ {
			if arr2[i] > arr2[j] {
				arr2[i], arr2[j] = arr2[j], arr2[i] // 交换位置
			}
		}
	}
	fmt.Println(arr2) //[1 4 6 7 8 9]
	/*
		冒泡排序：进行从小到大排序 [2,6,4,1,9,3]
	*/
	var arr3 = []int{2, 6, 4, 1, 9, 3}
	for i := 0; i < len(arr3); i++ {
		for j := i + 1; j < len(arr3); j++ {
			if arr3[i] > arr3[j] {
				temp := arr3[i]
				arr3[i] = arr3[j]
				arr3[j] = temp
			}
		}
	}
	fmt.Println(arr3)
	/*
	  sort包
	*/
	intList := []int{2, 5, 7, 3, 1, 0}
	float64List := []float64{2.5, 5.7, 3.1, 1.0, 0.0}
	stringList := []string{"a", "b", "g", "d", "e", "c"}
	//升序
	sort.Ints(intList)
	sort.Float64s(float64List)
	sort.Strings(stringList)
	fmt.Println(intList)
	fmt.Println(float64List)
	fmt.Println(stringList)
	/*
		[0 1 2 3 5 7]
		[0 1 2.5 3.1 5.7]
		[a b c d e g]
	*/

	//降序
	sort.Sort(sort.Reverse((sort.IntSlice(intList))))
	sort.Sort(sort.Reverse((sort.Float64Slice(float64List))))
	sort.Sort(sort.Reverse((sort.StringSlice(stringList))))
	fmt.Println(intList)
	fmt.Println(float64List)
	fmt.Println(stringList)
	/*
	 [7 5 3 2 1 0]
	 [5.7 3.1 2.5 1 0]
	 [g e d c b a]
	*/
}
